package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.GoodsPriceMapper;
import com.gxa.zhonglong.mapper.GoodsWeightMapper;
import com.gxa.zhonglong.pojo.GoodsPrice;
import com.gxa.zhonglong.pojo.GoodsWeight;
import com.gxa.zhonglong.service.GoodsPriceService;
import com.gxa.zhonglong.service.GoodsWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author NITRO
 */

@Service("goodsWeightService")
@Transactional(rollbackFor = {Throwable.class})
public class GoodsWeightServiceImpl implements GoodsWeightService {

    @Autowired
   private GoodsWeightMapper goodsWeightMapper;

    @Override
    public Boolean addGoodsWeight(GoodsWeight goodsWeight) throws BusinessException {
        GoodsWeight goodsWeight1 = goodsWeightMapper.selectGoodsWeightByCode(goodsWeight.getCode());
        if(goodsWeight1==null){
            goodsWeightMapper.addWeight(goodsWeight);
            return true;
        }else{
            throw new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
    }

    @Override
    public void deleteGoodsWeight(List<Long> ids) throws BusinessException {
        if (goodsWeightMapper.deleteWeight(ids)<1) {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }

    @Override
    public Boolean updateGoodsWeight(GoodsWeight goodsWeight) throws BusinessException {
        Boolean aBoolean = goodsWeightMapper.updateWeight(goodsWeight);
        if(aBoolean){
            return true;
        }else{
            throw  new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
    }

    @Override
    public List<GoodsWeight> getGoodsWeightList() {
        List<GoodsWeight> goodsWeights = goodsWeightMapper.selectGoodsWeight();
        return goodsWeights;
    }
}
